package cn.wolfcode.plus.mapper;

import cn.wolfcode.plus.domain.Employee;
import cn.wolfcode.plus.domain.Role;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * Author wyy
 * Date 2021/2/1 9:03
 **/
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
    //查所有员工
    @Select("select e.*, d.id d_id, d.name d_name, d.sn d_sn from employee e " +
            "left join department d on e.dept_id = d.id")
    @Results({
            @Result(column="d_id", property = "dept.id"),
            @Result(column="d_name", property = "dept.name"),
            @Result(column="d_sn", property = "dept.sn")
    })
    IPage<Employee> selectForList(IPage<Employee> page, @Param(Constants.WRAPPER) Wrapper<Employee> queryWrapper);

    //登录校验
    @Select("select e.* from employee e " +
            "where username = #{username} and password = #{password}")
    Employee checkLogin(@Param("username") String username, @Param("password") String password);

    //查指定员工的所有权限
    @Select("SELECT p.expression from employee_role er " +
            "join role_permission rp on er.role_id=rp.role_id " +
            "join permission p on rp.permission_id=p.id " +
            "where er.employee_id = #{id}")
    List<String> selectPermissionsByEId(Long id);

    @Select("select r.id, r.name, r.sn from role r " +
            "join employee_role er on r.id=er.role_id " +
            "where er.employee_id = #{id}")
    List<Role> selectRolesByEId(Long id);

    @Select("delete from employee_role where employee_id = #{id}")
    void deleteRelation(Long id);

    @Select("insert into employee_role (employee_id, role_id) " +
            "values (#{eid}, #{rid})")
    void insertRelation(@Param("eid") Long eid, @Param("rid") Long rid);

    @Select("select * from employee where username=#{username}")
    Employee selectByUsername(String username);

}
